/**	
 * <br>
 * Copyright 2012 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu <br>
 * FileName: MainActivity.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2012-11-15
 * @last Modified 
 * @history
 */

package com.chuan.mantu;

import java.util.Locale;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.chuan.mantu.extend.InfoTipAsyncTask;
import com.chuan.mantu.utils.Const;
import com.chuan.mantu.utils.MoveBg;

/**
 * {主操作界面}
 *  
 *  @author javacyc@qq.com
 *  @created 2012-11-15 下午02:44:01
 *  @lastModified       
 *  @history           
 */

public class MainActivity extends TabActivity {

	TabHost tabHost;
	TabHost.TabSpec tabSpec;
	RadioGroup radioGroup;
	RelativeLayout bottom_layout;
	ImageView img;
	int startLeft;
	int lastCheckedId;
	private InfoTipAsyncTask infoTipAsyncTask;
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			if(infoTipAsyncTask == null || infoTipAsyncTask.getStatus().equals(AsyncTask.Status.FINISHED)){
				infoTipAsyncTask = new InfoTipAsyncTask(this, R.string.info_tip_exit);
				infoTipAsyncTask.execute("");
				return false;
			}else if(infoTipAsyncTask != null && infoTipAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING)){
				System.exit(0);//退出
			}
		}
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		bottom_layout = (RelativeLayout) findViewById(R.id.layout_bottom);
		
        tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec(getString(R.string.pic)).setIndicator(getString(R.string.Pic)).setContent(new Intent(this,PicActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(getString(R.string.upload)).setIndicator(getString(R.string.Upload)).setContent(new Intent(this, UploadActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(getString(R.string.config)).setIndicator(getString(R.string.Config)).setContent(new Intent(this, ConfigActivity.class)));
        
        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(checkedChangeListener);
        
        img = new ImageView(this);
        img.setImageResource(R.drawable.tab_front_bg);
        bottom_layout.addView(img);
        lastCheckedId = radioGroup.getCheckedRadioButtonId();

	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_pic:
				lastCheckedId = R.id.radio_pic;
				Const.IF_BITMAP_RECYCLE = true;
				tabHost.setCurrentTabByTag(getString(R.string.pic));
				MoveBg.moveFrontBg(img, startLeft, 0, 0, 0);
				startLeft = 0;
				break;
			case R.id.radio_upload:
				lastCheckedId = R.id.radio_upload;
				Const.IF_BITMAP_RECYCLE = false;
				tabHost.setCurrentTabByTag(getString(R.string.upload));
				MoveBg.moveFrontBg(img, startLeft, img.getWidth(), 0, 0);
				startLeft = img.getWidth();
				break;
			case R.id.radio_config:
				lastCheckedId = R.id.radio_config;
				Const.IF_BITMAP_RECYCLE = false;
				tabHost.setCurrentTabByTag(getString(R.string.config));
				MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0);
				startLeft = img.getWidth() * 2;
				break;
			case R.id.radio_help:
				group.check(lastCheckedId);
				Const.IF_BITMAP_RECYCLE = false;
				StringBuilder sb = new StringBuilder();
				if(Locale.getDefault().getLanguage().equals("zh")){
					sb.append("欢迎使用本软件:\n");
					sb.append("1.软件需要下载大量图片,建议您最好在Wifi下使用.\n");
					sb.append("2.软件每次加载5张图片,你可以左右滑动浏览图片.\n");
					sb.append("3.上下滑动图片可以浏览历史图片和下载新图片.\n");
					sb.append("4.上传图片每次仅限单张,最大512KB.\n");
					sb.append("5.由于软件问题偶尔会造成图片展现失真问题,您可以通过手机图片软件进行浏览.\n");
					sb.append("6.下载的图片将会保存在您的SD卡上,您可以删除,复制,但请勿添加,重命名.\n");
					sb.append("7.如果您想更好的浏览图片,建议使用本地图片浏览软件.\n");
					sb.append("8.对于使用后带来的不适深表歉意.\n");
					sb.append("非常感谢您使用本软件.\n");
					sb.append("联系邮箱:greenshuai@yahoo.com.cn\n");
				}else{
					sb.append("Welcome to use the software:\n");
					sb.append("1.The software need download large pictures,we suggest used in Wifi.\n");
					sb.append("2.The software load five pictures,you can browse with Left-Right slide.\n");
					sb.append("3.You can up slide browse history pictures,down slide update the picture.\n");
					sb.append("4.Upload picture just one and one,the max size is 512KB.\n");
					sb.append("5.Due to a software problem occasionally casue image distortion,you can use the local picture browse software.\n");
					sb.append("6.The picture is saved in SD-card,you can delete,copy,but not add,rename.\n");
					sb.append("7.If you want a better browsing pictures,recommend the use of local image browser software.\n");
					sb.append("8.For the use of discomfort apologize.\n");
					sb.append("Thank you very much again.\n");
					sb.append("Email:greenshuai@yahoo.com.cn\n");
				}
				new AlertDialog.Builder(MainActivity.this)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setMessage(sb)
					.setPositiveButton(getString(R.string.button_confirm), null)
					.show();
				break;
			case R.id.radio_exit:
				System.exit(0);//退出
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}